extends Node2D


@onready var sprite_2d_original: Sprite2D = $Sprite2DOriginal as Sprite2D
@onready var sprite_2d_gray: Sprite2D = $Sprite2DGray as Sprite2D
@onready var spin_box_red: SpinBox = $SpinBoxRed as SpinBox
@onready var spin_box_green: SpinBox = $SpinBoxGreen as SpinBox
@onready var spin_box_blue: SpinBox = $SpinBoxBlue as SpinBox
@onready var check_box_convert: CheckBox = $CheckBoxConvert as CheckBox
@onready var option_button_generate: OptionButton = $OptionButtonGenerate as OptionButton


## 原始颜色
@export_color_no_alpha var original_color: Color = Color8(255, 128, 0, 255)


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	_get_original_color_channel_value()


# 获取原始颜色分量
func _get_original_color_channel_value() -> void:
	spin_box_red.value = original_color.r8
	spin_box_green.value = original_color.g8
	spin_box_blue.value = original_color.b8


#获取灰度图
func _get_gray_image() -> Image:
	if check_box_convert.button_pressed:
		return _convert_to_gray_image()
	else:
		return _generate_gray_image()

# 生成灰度图
func _generate_gray_image() -> Image:
	var option: int = option_button_generate.get_selected_id()
	var gray: int
	if option == 0:
		# HSL
		gray = int((original_color.r8 + original_color.g8 + original_color.b8) / 3.0)
	elif option == 1:
		# REC.709
		gray = int((0.21 * original_color.r8 + 0.72 * original_color.g8 + 0.07 * original_color.b8))
	elif option == 2:
		# REC.601
		gray = int((0.3 * original_color.r8 + 0.59 * original_color.g8 + 0.11 * original_color.b8))
	else:
		# godot Image._convert source code, equal to REC.709
		# gray = (0.21*65536*r + 0.72*65536*g + 0.07*65536*b + 32768) / 65536
		# 32768用于四舍五入，右移16位等于除以65536
		gray = (13938 * original_color.r8 + 46869 * original_color.g8 + 4729 * original_color.b8 + 32768) >> 16
	var gray_image: Image = Image.create_empty(1, 1, false, Image.FORMAT_L8)
	var gray_color: Color = Color8(gray, gray, gray, 255)
	gray_image.set_pixel(0, 0, gray_color)
	return gray_image

# 转换成灰度图
func _convert_to_gray_image() -> Image:
	if not sprite_2d_original.texture:
		return null
	var image: Image = sprite_2d_original.texture.get_image()
	if not image:
		return null
	if image.detect_alpha() == Image.ALPHA_BLEND:
		image.convert(Image.FORMAT_LA8)
	else:
		image.convert(Image.FORMAT_L8)
	return image


func _set_gray_image_texture() -> void:
	var image: Image = _get_gray_image()
	if not image:
		sprite_2d_gray.texture = null
	else:
		sprite_2d_gray.texture = ImageTexture.create_from_image(image)


func _change_color() -> void:
	original_color = Color8(int(spin_box_red.value), int(spin_box_green.value), int(spin_box_blue.value), 255)
	_set_original_image_texture()
	_set_gray_image_texture()
	
	
func _set_original_image_texture() -> void:
	var image: Image = Image.create_empty(1, 1, false, Image.FORMAT_RGB8)
	image.set_pixel(0, 0, original_color)
	sprite_2d_original.texture = ImageTexture.create_from_image(image)

func _on_spin_box_red_value_changed(_value: float) -> void:
	_change_color()


func _on_spin_box_green_value_changed(_value: float) -> void:
	_change_color()


func _on_spin_box_blue_value_changed(_value: float) -> void:
	_change_color()
